package com.visualexpress.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.shards.strategy.ShardStrategyFactory;

import com.visualexpress.domain.Location;
import com.visualexpress.domain.RouteInfo;
import com.visualexpress.shards.GlobalShardStrategyFactory;
import com.visualexpress.shards.HibernateSessionFactory;
import com.visualexpress.shards.VEShardStrategyFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * RouteInfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.visualexpress.domain.RouteInfo
 * @author MyEclipse Persistence Tools
 */

public class RouteInfoDAO implements IBaseHibernateDAO {

	private static final Log log = LogFactory.getLog(RouteInfoDAO.class);
	// property constants
	private static ShardStrategyFactory ssf;
	public static final String START_LOCATION = "startLocation";
	public static final String END_LOCATION = "endLocation";
	public static final String TIME = "time";
	public static final String FLUENT = "fluent";
	public static final String WEIGHT = "weight";
	public static final String RECENT_TIME = "recentTime";

	public void save(RouteInfo transientInstance) {
		log.debug("saving RouteInfo instance");
		try {
			Session s = this.getSession();
			Transaction t = s.beginTransaction();
			s.save(transientInstance);
			s.flush();
			t.commit();
			System.out.println(transientInstance.getId());
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
    public List<RouteInfo> findByStartAndEnd(Location start,Location end)
    {
    	log.debug("query RouteInfo instance");
    	try{
    		String hql = "from RouteInfo as ri where ri.startLocation.id=? and ri.endLocation.id=?";
    		Query query = this.getSession().createQuery(hql);
    		query.setParameter(0, start.getId());
    		query.setParameter(1, end.getId());
    		List<RouteInfo> ris = query.list();
    		log.debug("query successful!");
    		return ris;
    	}catch (RuntimeException re) {
			log.error("query failed", re);
			throw re;
		}
    }
	public void delete(RouteInfo persistentInstance) {
		log.debug("deleting RouteInfo instance");
		try {
			Session s = this.getSession();
			Transaction t = s.beginTransaction();
			s.delete(persistentInstance);
			t.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public RouteInfo findById(java.lang.Long id) {
		log.debug("getting RouteInfo instance with id: " + id);
		try {
			RouteInfo instance = (RouteInfo) getSession().get(
					"com.visualexpress.domain.RouteInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(RouteInfo instance) {
		log.debug("finding RouteInfo instance by example");
		try {
			List results = getSession().createCriteria(
					"com.visualexpress.domain.RouteInfo").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding RouteInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from RouteInfo as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByStartLocation(Object startLocation) {
		return findByProperty(START_LOCATION, startLocation);
	}

	public List findByEndLocation(Object endLocation) {
		return findByProperty(END_LOCATION, endLocation);
	}

	public List findByTime(Object time) {
		return findByProperty(TIME, time);
	}

	
	public List findByFluent(Object fluent) {
		return findByProperty(FLUENT, fluent);
	}
	
	public List findByWeight(Object weight) {
		return findByProperty(WEIGHT, weight);
	}
	
	public List findByRecentTime(Object recentTime) {
		return findByProperty(RECENT_TIME, recentTime);
	}

	public List findAll() {
		log.debug("finding all RouteInfo instances");
		try {
			String queryString = "from RouteInfo";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public RouteInfo merge(RouteInfo detachedInstance) {
		log.debug("merging RouteInfo instance");
		try {
			RouteInfo result = (RouteInfo) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(RouteInfo instance) {
		log.debug("attaching dirty RouteInfo instance");
		try {
			Session s = this.getSession();
			Transaction t = s.beginTransaction();
			s.saveOrUpdate(instance);
			t.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
			}
	}

	public void attachClean(RouteInfo instance) {
		log.debug("attaching clean RouteInfo instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	@Override
	public Session getSession() {
		if(ssf==null)
			ssf = new GlobalShardStrategyFactory();
		return HibernateSessionFactory.getSession(ssf);
	}
}
